﻿using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Controls;
using FluentAvalonia.UI.Controls;
using iOSClub.App.Desktop.Models;
using iOSClub.App.Desktop.ViewModels.Pages;
using iOSClub.App.Share;

namespace iOSClub.App.Desktop.Views;

public partial class MainView : UserControl
{
    private readonly Dictionary<string, PageModelBase> Stack = [];
    public string Jwt { get; }
    public MemberModel User { get; }

    private NavigationViewItem? _currentItem;

    public MainView(string jwt, MemberModel user, SettingModel setting)
    {
        Jwt = jwt;
        User = user;
        var home = new HomePageModel() { User = user };
        foreach (var s in setting.Staples)
        {
            home.NavigationList.Add(s);
        }
        Stack.Add("Home", home);
        Stack.Add("Tool", new ToolPageModel());
        Stack.Add("Weather", new WeatherPageModel());
        Stack.Add("Setting", new SettingPageModel() { User = user });
        InitializeComponent();

        if (user.Identity != "Member")
        {
            Stack.Add("Project", new ProjectPageModel());
            ProjectItem.IsVisible = true;
        }

        if (user.Identity.Contains("Minister")
            || user.Identity == "President" || user.Identity == "Founder")
        {
            Stack.Add("Data", new ManagePageModel());
            DataItem.IsVisible = true;
        }

        var now = DateTime.Now;
        if (now.Hour is >= 18 or <= 6)
        {
            WeatherIcon.Symbol = Symbol.WeatherMoon;
        }

        NavView.SelectedItem = _currentItem = NavView.MenuItems[0] as NavigationViewItem;
        Navigate("Home");
    }

    private void ItemInvoked(object? sender, NavigationViewItemInvokedEventArgs e)
    {
        if (e.InvokedItemContainer.Tag is not string s) return;
        if (e.InvokedItemContainer is not NavigationViewItem item) return;
        if (item.Content is SymbolIcon icon)
        {
            icon.Symbol = GetSymbol(icon.Symbol, false);

            if (_currentItem is { Content: SymbolIcon _currentIcon })
                _currentIcon.Symbol = GetSymbol(_currentIcon.Symbol);
        }

        Navigate(s);

        _currentItem = item;
    }

    private static Symbol GetSymbol(Symbol page, bool isPre = true)
    {
        var iconString = page.ToString();
        if (isPre)
        {
            if (iconString.Contains("Filled")) iconString = iconString.Replace("Filled", "");
        }
        else
        {
            if (!iconString.Contains("Filled")) iconString += "Filled";
        }

        return Enum.TryParse<Symbol>(iconString, out var symbolIcon) ? symbolIcon : page;
    }

    private void Navigate(string page)
    {
        if (string.IsNullOrEmpty(page)) return;
        Navigate(Stack[page]);
    }

    public void Navigate(object context)
    {
        if (context is Control control)
        {
            NavView.Content = control;
            return;
        }

        NavView.Content = context is not PageModelBase model
            ? new TextBlock() { Text = "暂不支持" }
            : model.Init();
    }

    public void Nav(string page)
    {
        if (string.IsNullOrEmpty(page)) return;

        var list = NavView.MenuItems.OfType<NavigationViewItem>().ToArray();

        var item = list.FirstOrDefault(x => x.Tag is string s && s == page);
        if (item is null) return;

        _currentItem = NavView.SelectedItem as NavigationViewItem;

        if (item.Content is SymbolIcon icon)
        {
            icon.Symbol = GetSymbol(icon.Symbol, false);

            if (_currentItem is { Content: SymbolIcon _currentIcon })
                _currentIcon.Symbol = GetSymbol(_currentIcon.Symbol);
        }

        NavView.SelectedItem = item;

        Navigate(page);
    }
}